//
//  EngineerTableViewController2.m
//  WZYInteriorDesign
//
//  Created by lanou on 15/12/19.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "EngineerTableViewController2.h"
#import "LookDesign.h"
#import "LookDesignTableViewCell.h"
#import "UIScrollView+MJRefresh.h"
#import "MainDetailViewController.h"
@interface EngineerTableViewController2 ()

@property (nonatomic, strong)NSMutableArray *mainArray;

@property (nonatomic, assign)NSInteger *number;

@end

@implementation EngineerTableViewController2

- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = NO;
}

//懒加载
- (NSMutableArray *)mainArray{
    if (!_mainArray) {
        _mainArray = [NSMutableArray array];
    }
    return _mainArray;
}

//解析数据
- (void)jsonLoonEngineer{
    NSString *urlString = [NSString stringWithFormat:@"http://api.meilijia.com/app/pro_gz_work_list?city_id=&page=%ld",self.number];
    [LORequestManger GET:urlString success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSArray *rootArray = dic[@"data"][@"pro_gz_work_list"][@"collections"];
        for (NSDictionary *collectionsDic in rootArray) {
            LookDesign *lookEngineer = [LookDesign shareJsonWithFindDesign:collectionsDic];
            [self.mainArray addObject:lookEngineer];
        }
        //NSLog(@"%@",self.mainArray);
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.number = 1;
    [self jsonLoonEngineer];
    self.view.backgroundColor = [UIColor yellowColor];
    
    //注册单元格
    [self.tableView registerClass:[LookDesignTableViewCell class] forCellReuseIdentifier:@"cell1"];
    
    __weak EngineerTableViewController2 *mainView = self;
    [mainView.tableView addHeaderWithCallback:^{
        NSLog(@"上拉刷新");
        [mainView jsonLoonEngineer];
        [mainView.tableView headerEndRefreshing];
    }];
    
    [mainView.tableView addFooterWithCallback:^{
        NSLog(@"上拉刷新");
        mainView.number += 1;
        [mainView jsonLoonEngineer];
        [mainView.tableView footerEndRefreshing];
    }];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.mainArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LookDesignTableViewCell *lookEngineer = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    lookEngineer.lookDesign = self.mainArray[indexPath.row];
   return lookEngineer;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 330;
}

//点击后跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MainDetailViewController *mainVC = [[MainDetailViewController alloc]init];
    mainVC.mailLabel = _mainArray[indexPath.row];
    [self presentViewController:mainVC animated:YES completion:nil];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
